<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="代理姓名" prop="agentName">
                <el-input v-model="queryParams.agentName" placeholder="请输入代理姓名"
                        clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="手机号" prop="agentPhone">
                <el-input v-model="queryParams.agentPhone" placeholder="请输入手机号"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="银行卡号" prop="bankNo">
                <el-input v-model="queryParams.bankNo" placeholder="请输入银行卡号"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker v-model="dateRange" size="small" style="width: 360px" value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetimerange" range-separator="-" :default-time="['00:00:00', '23:59:59']" start-placeholder="开始日期" end-placeholder="结束日期"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请输入状态"
                    clearable
                    size="small"
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in sysStatus"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                           v-hasPermi="['bs:agent:save']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                        v-hasPermi="['bs:agent:export']"
                >导出</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
            <el-table-column label="ID" align="center" prop="id" :show-overflow-tooltip="true" />
            <el-table-column label="代理姓名" align="center" prop="agentName" :show-overflow-tooltip="true" />
            <el-table-column label="手机号" align="center" prop="agentPhone" :show-overflow-tooltip="true" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
            <el-table-column label="客户数" align="center" prop="customerNum" :show-overflow-tooltip="true" />
            <el-table-column label="银行卡号" align="center" prop="bankNo" :show-overflow-tooltip="true" />
            <el-table-column label="开户银行" align="center" prop="bankDesc" :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="status" width="100">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-value="0"
                        inactive-value="1"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-key" @click="handleResetPwd(scope.row)"
                               v-hasPermi="['bs:agent:resetPwd']">重置密码</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['bs:agent:saveOrUpdate']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['bs:agent:delete']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.nowPage" :limit.sync="queryParams.pageSize" @pagination="getPage"/>
        <!-- 查看代理管理详情对话框 -->
        <el-dialog :title="title" :visible.sync="showOpen" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="showForm" :model="showForm" :rules="rules" label-width="120px" >
                <el-form-item label="代理姓名" prop="agentName">
                    <el-input v-model="showForm.agentName" style="width: 450px;"  type="text"  disabled placeholder="请输入代理姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="agentPhone">
                    <el-input v-model="showForm.agentPhone" style="width: 450px;"  type="text"  disabled placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="登陆密码" prop="password">
                    <el-input v-model="showForm.password" style="width: 450px;"  type="text"  disabled placeholder="请输入登陆密码" />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bankNo">
                    <el-input v-model="showForm.bankNo" style="width: 450px;"  type="text"  disabled placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bankDesc">
                    <el-input v-model="showForm.bankDesc" style="width: 450px;"  type="text"  disabled placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="showForm.status">
                        <el-radio
                            v-for="dict in sysStatus"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelShow">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改代理管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="代理姓名" prop="agentName">
                    <el-input v-model="form.agentName" style="width: 450px;" type="text"  placeholder="请输入代理姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="agentPhone">
                    <el-input v-model="form.agentPhone" style="width: 450px;"  type="text" placeholder="请输入手机号" @change="setPassword"/>
                </el-form-item>
                <el-form-item label="登陆密码" prop="password" v-show="openStatue !== 'edit'">
                    <el-input v-model="form.password" style="width: 450px;"  type="text" placeholder="请输入登陆密码" />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bankNo">
                    <el-input v-model="form.bankNo" style="width: 450px;"  type="text" placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bankDesc">
                    <el-input v-model="form.bankDesc" style="width: 450px;"  type="text" placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="dict in sysStatus"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {getBsAgentPage,getBsAgentById,addOrUpdateBsAgent,delBsAgent,exportBsAgent, resetAgentPwd} from "@/api/project/bsAgent/bsAgent";
import {resetUserPwd} from "@/api/system/user";
export default {
    name: "bsAgent",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            open: false,// 是否显示修改或增加的弹出层
            openStatue: "",
            // 日期范围
            dateRange: [],
            // 状态数据字典
            sysStatus: [],
            // 查询参数
            queryParams: {
                nowPage: 1,
                pageSize: 10,
                agentName: undefined,
                bankDesc: undefined,
                bankNo: undefined,
                createBy: undefined,
                customerNum: undefined,
                password: undefined,
                agentPhone: undefined,
                status: undefined,
                updateBy: undefined,
            },
            // 表单参数
            form: {},
            showOpen: false,//是否显示查看工作内容弹出层
            showForm: {},
            // 表单校验
            rules: {
                agentName: [
                    {required: true, message: "代理姓名不能为空", trigger: "blur"}
                ],
                bankDesc: [
                    {required: true, message: "开户银行不能为空", trigger: "blur"}
                ],
                bankNo: [
                    {required: true, message: "银行卡号不能为空", trigger: "blur"}
                ],
                password: [
                    {required: true, message: "登陆密码不能为空", trigger: "blur"}
                ],
                agentPhone: [
                    {required: true, message: "手机号不能为空", trigger: "blur"},
                    {
                        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                        message: "请输入正确的手机号码",
                        trigger: "blur"
                    }
                ],
                status: [
                    {required: true, message: "状态不能为空", trigger: "blur"}
                ],
            },
        };
    },
    created() {
        this.getPage();
        this.getDicts("sys_normal_disable").then(response => {
            this.sysStatus = response.data;
        });
    },
    methods: {
        /** 查询参数列表 */
        getPage(e) {
            this.loading = true;
            let agentName = this.queryParams.agentName || '';
            let bankDesc = this.queryParams.bankDesc || '';
            let bankNo = this.queryParams.bankNo || '';
            let createBy = this.queryParams.createBy || '';
            let customerNum = this.queryParams.customerNum || '';
            let password = this.queryParams.password || '';
            let agentPhone = this.queryParams.agentPhone || '';
            let status = this.queryParams.status || '';
            let updateBy = this.queryParams.updateBy || '';
            let time = this.dateRange || '';
            let params = '';
            if (e) {
                params = {
                    agentName: agentName,
                    bankDesc: bankDesc,
                    bankNo: bankNo,
                    createBy: createBy,
                    customerNum: customerNum,
                    password: password,
                    agentPhone: agentPhone,
                    status: status,
                    updateBy: updateBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: e.page,
                    pageSize: e.limit
                }
            } else {
                params = {
                    agentName: agentName,
                    bankDesc: bankDesc,
                    bankNo: bankNo,
                    createBy: createBy,
                    customerNum: customerNum,
                    password: password,
                    agentPhone: agentPhone,
                    status: status,
                    updateBy: updateBy,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: this.queryParams.nowPage,
                    pageSize: this.queryParams.pageSize
                }
            }
            getBsAgentPage(params).then(response => {
                this.configList = response.data.records;
                this.total = response.data.total;
                this.loading = false;
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                agentName: undefined,
                bankDesc: undefined,
                bankNo: undefined,
                createBy: undefined,
                createTime: undefined,
                customerNum: undefined,
                password: undefined,
                agentPhone: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.showForm = {
                agentName: undefined,
                bankDesc: undefined,
                bankNo: undefined,
                createBy: undefined,
                createTime: undefined,
                customerNum: undefined,
                password: undefined,
                agentPhone: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.resetForm("form");
            this.resetForm("showForm");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            let agentName = this.queryParams.agentName || '';
            let bankDesc = this.queryParams.bankDesc || '';
            let bankNo = this.queryParams.bankNo || '';
            let createBy = this.queryParams.createBy || '';
            let customerNum = this.queryParams.customerNum || '';
            let password = this.queryParams.password || '';
            let agentPhone = this.queryParams.agentPhone || '';
            let status = this.queryParams.status || '';
            let updateBy = this.queryParams.updateBy || '';
            let time = this.dateRange || '';
            let params = {
                agentName: agentName,
                bankDesc: bankDesc,
                bankNo: bankNo,
                createBy: createBy,
                customerNum: customerNum,
                password: password,
                agentPhone: agentPhone,
                status: status,
                updateBy: updateBy,
                startTime: time[0],
                endTime: time[1],
                nowPage: 1,
                pageSize: this.queryParams.pageSize
            };
            getBsAgentPage(params).then(res => {
                if (res.code == 200) {
                    this.configList = res.data.records;
                    this.total = res.data.total;
                    this.loading = false;
                }
            })
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.queryParams.agentName = '';
            this.queryParams.bankDesc = '';
            this.queryParams.bankNo = '';
            this.queryParams.createBy = '';
            this.queryParams.customerNum = '';
            this.queryParams.password = '';
            this.queryParams.agentPhone = '';
            this.queryParams.status = '';
            this.queryParams.updateBy = '';
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.openStatue = "add";
            this.form.status = '0';
            this.open = true;
            this.title = "添加代理";
        },
        //多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length
        },
        /** 查看按钮操作 */
        handleShow(row) {
            this.reset();
            const id = row.id || '';
            getBsAgentById({id}).then(response => {
                this.showForm = response.data;
                this.showOpen = true;
                this.title = "代理管理详情";
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || '';
            this.openStatue = 'edit';
            getBsAgentById({id}).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改代理管理信息";
            });
        },
        cancelShow: function () {
            this.showOpen = false;
        },
        /** 状态修改 */
        handleStatusChange(row) {
            let text = row.status === "0" ? "启用" : "停用";
            this.$confirm('确认要"' + text + '""' + row.agentName + '"吗?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return addOrUpdateBsAgent(row);
            }).then(() => {
                this.msgSuccess(text + "成功");
            }).catch(function() {
                row.status = row.status === "0" ? "1" : "0";
            });
        },
        /** 重置密码按钮操作 */
        handleResetPwd(row) {
            this.$prompt('请输入"' + row.agentName + '"的新密码', "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(({
                         value
                     }) => {
                resetAgentPwd(row.id, row.agentPhone, value).then(response => {
                    if (response.code === "200") {
                        this.msgSuccess("修改成功，新密码是：" + value);
                    } else {
                        this.msgError(response.msg);
                    }
                });
                // }
            }).catch(() => {});
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id !== undefined) {
                        addOrUpdateBsAgent(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    } else {
                        addOrUpdateBsAgent(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除代理管理编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delBsAgent(ids);
            }).then(() => {
                this.getPage();
                this.msgSuccess("删除成功");
            }).catch(function () {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有代理管理数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportBsAgent(queryParams);
            }).then(res => {
                this.download(res.msg);
            }).catch(function () {
            });
        },
        /**
         * 设置默认密码
         */
        setPassword() {
            console.log(this.form)
            if(this.form.agentPhone.length >= 6) {
                this.form.password = this.form.agentPhone.substring(this.form.agentPhone.length - 6)
            }
        }
    }
};
</script>
<style>

</style>
